from ui import Button, RootPanel, HTML, DockPanel, HasAlignment, Hyperlink, VerticalPanel
import Window
from SinkList import SinkList
from History import History

import TIBlog
import TIPicture
import TIAbout

class godtime:

    def onHistoryChanged(self, token):
        info = self.sink_list.find(token)
        if info:
            self.show(info, False)
        else:
            self.showIntro()

    def onWindowResized(self,cx,cy):
       
        ofWidthRight = self.sink_list.vp_list.getOffsetWidth() + 3

        ofHeightRight = 0
        #self.description.getOffsetHeight()
        #Window.alert("waaaa:" + Window.getClientWidth()+Window.getClientHeight())
        self.sink_list.onSizeEx(cx-ofWidthRight,cy-ofHeightRight)
        RootPanel().setSize(cx, cy) 
    def onModuleLoad(self):
        Window.addWindowResizeListener(self)
        
        self.curInfo=''
        self.curSink=None

        self.sink_list=SinkList()
        self.panel=DockPanel()

        self.loadSinks()
        self.sinkContainer = DockPanel()
        self.sinkContainer.setStyleName("ks-Sink")

        vp=VerticalPanel()

        vp.add(self.sinkContainer)


        self.panel.add(self.sink_list, DockPanel.WEST)
        self.panel.add(vp, DockPanel.CENTER)
        
        self.panel.setCellVerticalAlignment(self.sink_list, HasAlignment.ALIGN_TOP)


        History().addHistoryListener(self)
        RootPanel().add(self.panel)


        self.showIntro()


    def show(self, info, affectHistory):
        if info == self.curInfo: return
        self.curInfo = info

        if self.curSink <> None:
            self.curSink.onHide()
            self.sinkContainer.remove(self.curSink)

        self.curSink = info.getInstance()
        self.sink_list.setSinkSelection(info.getName())
##        self.description.setHTML(info.getDescription())

        if (affectHistory):
            History().newItem(info.getName())

        self.sinkContainer.add(self.curSink, DockPanel.CENTER)
        self.sinkContainer.setCellVerticalAlignment(self.curSink, HasAlignment.ALIGN_TOP)
        self.curSink.onShow()

    def loadSinks(self):
        self.sink_list.addSink(TIBlog.init())
        self.sink_list.addSink(TIPicture.init())
        self.sink_list.addSink(TIAbout.init())
    def showIntro(self):
        
        self.onWindowResized(Window.getClientWidth(),Window.getClientHeight())
        self.show(self.sink_list.find("Blog"))

